package com.idanchuang.support.etcache;

import com.idanchuang.support.etcache.local.LocalCacheFactory;
import com.idanchuang.support.etcache.local.caffeine.CaffeineLocalCacheFactory;
import com.idanchuang.support.etcache.pressure.PressureChecker;
import com.idanchuang.support.etcache.pubsub.DefaultSubEventQueue;
import com.idanchuang.support.etcache.pubsub.SubEventQueue;
import com.idanchuang.support.etcache.remote.RemoteCacheFactory;
import com.idanchuang.support.etcache.remote.codec.FastjsonRemoteKeyCodec;
import com.idanchuang.support.etcache.remote.codec.RemoteKeyCodec;
import com.idanchuang.support.etcache.remote.redis.RedisRemoteCacheFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * @author yjy
 * Created at 2021/12/27 10:35 下午
 */
@Configuration
@ConditionalOnEtCacheEnabled
public class EtCacheCoreAutoConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "idanchuang.component.etcache")
    public EtCacheProperties etCacheProperties() {
        return new EtCacheProperties();
    }

    @Bean
    @ConditionalOnMissingBean(RemoteKeyCodec.class)
    public RemoteKeyCodec remoteKeyCodec() {
        return new FastjsonRemoteKeyCodec();
    }

    @Bean
    @ConditionalOnMissingBean(LocalCacheFactory.class)
    public LocalCacheFactory localCacheFactory() {
        return new CaffeineLocalCacheFactory();
    }

    @Bean
    @ConditionalOnMissingBean(RemoteCacheFactory.class)
    public RemoteCacheFactory remoteCacheFactory(RedisConnectionFactory redisConnectionFactory) {
        return new RedisRemoteCacheFactory(redisConnectionFactory);
    }

    @Bean
    @ConditionalOnMissingBean(PressureChecker.class)
    public PressureChecker pressureChecker() {
        return new PressureChecker.NonPressureChecker();
    }

    @Bean
    @ConditionalOnMissingBean(SubEventQueue.class)
    public SubEventQueue subEventQueue() {
        return new DefaultSubEventQueue();
    }

}
